Skip to contentMethod: static {...}
1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.model.metamodel;
19:
20: import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties;
21: import cz.cvut.kbss.jopa.oom.converter.ConverterWrapper;
22: import cz.cvut.kbss.jopa.oom.converter.ObjectConverter;
23: import cz.cvut.kbss.jopa.oom.converter.ToDoubleConverter;
24: import cz.cvut.kbss.jopa.oom.converter.ToFloatConverter;
25: import cz.cvut.kbss.jopa.oom.converter.ToIntegerConverter;
26: import cz.cvut.kbss.jopa.oom.converter.ToLangStringConverter;
27: import cz.cvut.kbss.jopa.oom.converter.ToLongConverter;
28: import cz.cvut.kbss.jopa.oom.converter.ToShortConverter;
29: import cz.cvut.kbss.jopa.oom.converter.ToStringConverter;
30: import cz.cvut.kbss.jopa.oom.converter.ToURIConverter;
31: import cz.cvut.kbss.jopa.oom.converter.ToURLConverter;
32: import cz.cvut.kbss.jopa.oom.converter.datetime.DateConverter;
33: import cz.cvut.kbss.jopa.oom.converter.datetime.InstantConverter;
34: import cz.cvut.kbss.jopa.oom.converter.datetime.LocalDateTimeConverter;
35: import cz.cvut.kbss.jopa.oom.converter.datetime.LocalTimeConverter;
36: import cz.cvut.kbss.jopa.oom.converter.datetime.ZonedDateTimeConverter;
37: import cz.cvut.kbss.jopa.utils.Configuration;
38: import cz.cvut.kbss.ontodriver.model.LangString;
39:
40: import java.net.URI;
41: import java.net.URL;
42: import java.time.Instant;
43: import java.time.LocalDateTime;
44: import java.time.LocalTime;
45: import java.time.ZonedDateTime;
46: import java.util.Date;
47: import java.util.HashMap;
48: import java.util.Map;
49: import java.util.Optional;
50:
51: /**
52: * Manages attribute converters.
53: */
54: public class Converters {
55:
56: private static final Map<Class<?>, ConverterWrapper<?, ?>> DEFAULT_CONVERTERS = initDefaultConverters();
57:
58: private final Map<Class<?>, ConverterWrapper<?, ?>> converters = new HashMap<>();
59:
60: Converters(Configuration configuration) {
61: initConverters(configuration);
62: }
63:
64: private void initConverters(Configuration configuration) {
65: converters.put(Object.class, new ObjectConverter(configuration.is(JOPAPersistenceProperties.PREFER_MULTILINGUAL_STRING)));
66: }
67:
68: Optional<ConverterWrapper<?, ?>> getCustomConverter(Class<?> attributeType) {
69: return Optional.ofNullable(converters.get(attributeType));
70: }
71:
72: void registerConverter(Class<?> attributeType, ConverterWrapper<?, ?> converter) {
73: converters.put(attributeType, converter);
74: }
75:
76: public static Optional<ConverterWrapper<?, ?>> getDefaultConverter(Class<?> attributeType) {
77: return Optional.ofNullable(DEFAULT_CONVERTERS.get(attributeType));
78: }
79:
80: private static Map<Class<?>, ConverterWrapper<?, ?>> initDefaultConverters() {
81: return Map.ofEntries(Map.entry(LocalDateTime.class, new LocalDateTimeConverter()),
82: Map.entry(LocalTime.class, new LocalTimeConverter()),
83: Map.entry(Instant.class, new InstantConverter()),
84: Map.entry(ZonedDateTime.class, new ZonedDateTimeConverter()),
85: Map.entry(Date.class, new DateConverter()),
86: Map.entry(Short.class, new ToShortConverter()),
87: Map.entry(Integer.class, new ToIntegerConverter()),
88: Map.entry(Long.class, new ToLongConverter()),
89: Map.entry(Float.class, new ToFloatConverter()),
90: Map.entry(Double.class, new ToDoubleConverter()),
91: Map.entry(String.class, new ToStringConverter()),
92: Map.entry(LangString.class, new ToLangStringConverter()),
93: Map.entry(URI.class, new ToURIConverter()),
94: Map.entry(URL.class, new ToURLConverter()));
95: }
96:
97: /**
98: * Gets a map of default, built-in converters.
99: *
100: * @return Map of built-in converters, where the key is the target attribute type
101: */
102: public static Map<Class<?>, ConverterWrapper<?, ?>> getDefaultConverters() {
103: return DEFAULT_CONVERTERS;
104: }
105: }